package com.example.travelhelper.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.example.travelhelper.mapper.PageViewMapper;
import com.example.travelhelper.mapper.WebMapper;
import com.example.travelhelper.pojo.PageView;
import com.example.travelhelper.pojo.Web;
import com.example.travelhelper.pojo.WebTitle;
import com.example.travelhelper.pojo.vo.LookMessage;
import com.example.travelhelper.service.WebService;
import com.example.travelhelper.utils.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;
import sun.java2d.pipe.SpanIterator;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Optional;

@Service
public class WebServiceImpl implements WebService {

    @Autowired
    private WebMapper webMapper;

    @Autowired
    private PageViewMapper pageViewMapper;
    //查询
    @Override
    public List<WebTitle> disPlay() {
        List<WebTitle> webTitles = webMapper.selectList(null);
        return webTitles;
    }

    @Override
    public ResponseData look(String ip) {

        try {
            pageViewMapper.insert(new PageView(ip,null));
            return ResponseData.success("第一次访问");
        }catch (Exception e){
            return ResponseData.fail("重复访问");
        }
        //  return ResponseData.success("访问失败");
    }

    @Override
    public ResponseData getlookCount(String begin,String end) {

        LocalDate endDate = LocalDate.now();


//        for (int i = 0; i <6 ; i++) {
//LookMessage lookCount = pageViewMapper.getLookCount(endDate.minusDays(i-1).toString(),endDate.minusDays(i).toString());
//            System.out.println(lookCount);
//        }
        List<LookMessage> lookCount = pageViewMapper.getLookCount(begin, end);
        System.out.println(lookCount);


        //  List<Map<String, Object>> rows = pageViewMapper.queryForList(sql, startDate, endDate);

        // 将查询结果存储到 Map 中
//        for (Map<String, Object> row : rows) {
//            LocalDate date = ((java.sql.Date) row.get("date_column")).toLocalDate();
//            Long count = (Long) row.get("COUNT(*)");
//            dailyCounts.put(date, count);
//        }

        return ResponseData.success("查询成功",lookCount);
    }


/*    //修改保存到数据库
    @Override
    public void update(WebTitle webTitle) {

        //获取当前时间
        LocalDate localDate = LocalDate.now();
        //定义日期格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        //格式化日期
        String formatDate = localDate.format(formatter);
        //更新时间传入
        webTitle.setWUpdatetime(formatDate);

        // 使用 LambdaUpdateWrapper 构造更新条件
        LambdaUpdateWrapper<WebTitle> wrapper = new LambdaUpdateWrapper<>();

        // 使用 Java 8 的 Optional 类型和方法引用，只设置属性不为空的字段
        Optional.ofNullable(webTitle.getWTitle()).ifPresent(wTitle -> wrapper.set(WebTitle::getWTitle, wTitle));
        Optional.ofNullable(webTitle.getWUpdatetime()).ifPresent(wUpdatetime -> wrapper.set(WebTitle::getWUpdatetime, wUpdatetime));
        Optional.ofNullable(webTitle.getWImageback()).ifPresent(wImageback -> wrapper.set(WebTitle::getWImageback, wImageback));
        Optional.ofNullable(webTitle.getWImagehead()).ifPresent(wImagehead -> wrapper.set(WebTitle::getWImagehead, wImagehead));
        Optional.ofNullable(webTitle.getWDetail()).ifPresent(wDetail -> wrapper.set(WebTitle::getWDetail, wDetail));

        Optional.ofNullable(webTitle.getWDay()).ifPresent(wDay -> wrapper.set(WebTitle::getWDay, wDay));
        Optional.ofNullable(webTitle.getWTime()).ifPresent(wTime -> wrapper.set(WebTitle::getWTime, wTime));
        Optional.ofNullable(webTitle.getWCost()).ifPresent(wCost -> wrapper.set(WebTitle::getWCost, wCost));
        Optional.ofNullable(webTitle.getWPeople()).ifPresent(wPeople -> wrapper.set(WebTitle::getWPeople, wPeople));

        // 设置更新条件
        wrapper.eq(WebTitle::getWId, webTitle.getWId());
        // 执行更新操作
        webMapper.update(null,wrapper);
    }*/

    //修改保存到数据库
    @Override
    public ResponseData update(WebTitle webTitle) {

        //获取当前时间
        LocalDate localDate = LocalDate.now();
        //定义日期格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        //格式化日期
        String formatDate = localDate.format(formatter);
        //更新时间传入
        webTitle.setWUpdatetime(formatDate);

        // 使用 LambdaUpdateWrapper 构造更新条件
        LambdaUpdateWrapper<WebTitle> wrapper = new LambdaUpdateWrapper<>();

        // 使用 Java 8 的 Optional 类型和方法引用，只设置属性不为空的字段
        Optional.ofNullable(webTitle.getWTitle()).ifPresent(wTitle -> wrapper.set(WebTitle::getWTitle, wTitle));
        Optional.ofNullable(webTitle.getWUpdatetime()).ifPresent(wUpdatetime -> wrapper.set(WebTitle::getWUpdatetime, wUpdatetime));
        Optional.ofNullable(webTitle.getWImageback()).ifPresent(wImageback -> wrapper.set(WebTitle::getWImageback, wImageback));
        Optional.ofNullable(webTitle.getWImagehead()).ifPresent(wImagehead -> wrapper.set(WebTitle::getWImagehead, wImagehead));
        Optional.ofNullable(webTitle.getWDetail()).ifPresent(wDetail -> wrapper.set(WebTitle::getWDetail, wDetail));

        Optional.ofNullable(webTitle.getWDay()).ifPresent(wDay -> wrapper.set(WebTitle::getWDay, wDay));
        Optional.ofNullable(webTitle.getWTime()).ifPresent(wTime -> wrapper.set(WebTitle::getWTime, wTime));
        Optional.ofNullable(webTitle.getWCost()).ifPresent(wCost -> wrapper.set(WebTitle::getWCost, wCost));
        Optional.ofNullable(webTitle.getWPeople()).ifPresent(wPeople -> wrapper.set(WebTitle::getWPeople, wPeople));

        // 设置更新条件
        wrapper.eq(WebTitle::getWId, webTitle.getWId());
        // 执行更新操作
        int i = webMapper.update(null, wrapper);
        System.out.println(i);
        if (i!=0){
            return ResponseData.success("后台标题修改！");
        }else {
            return ResponseData.fail("后台标题修改失败！");
        }
    }
}
